/*
 * 文件名：SysRoleController.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-8-20
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.rbac.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qbgs.base.BaseResult;
import com.qbgs.base.SimpleBaseResult;
import com.qbgs.base.TreeEntity;
import com.qbgs.constant.GlobalConstant;
import com.qbgs.rbac.service.MenuService;
import com.qbgs.rbac.service.RoleService;


/**
 * 
 * @author will_awoke
 * @version 2014-8-20
 * @see SysRoleController
 * @since
 */
@Controller
@RequestMapping(value = "sysRole")
public class SysRoleController
{

    /**
     * 日志
     */
    private static final Logger log = Logger.getLogger(SysRoleController.class);

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    /**
     * 菜单http跳转角色管理页面
     * @return 
     * @see
     */
    @RequestMapping(value = "/toView.do")
    public String toView()
    {
        return "rbac/sysRole";
    }

    /**
     * ajax分页查询。
     * 方法体不throws exception
     * 使用try catch，封装操作提示语，用于页面ajax提示
     * @return 
     * @see
     */
    @RequestMapping(value = "/roleList.do")
    @ResponseBody
    public BaseResult roleList(HttpServletRequest request)
    {
        BaseResult result = new BaseResult();
        try
        {
            //page：当前页    rows：每页显示记录条数  区别结果集存放List-rows
            int page = ServletRequestUtils.getIntParameter(request, "page", 1);
            int rows = ServletRequestUtils.getIntParameter(request, "rows", 10);
            String roleName = ServletRequestUtils.getStringParameter(request, "role_name", "");

            result = roleService.queryRolePage(page, rows, roleName.trim());
        }
        catch (Exception e)
        {
            //出现异常，则捕获该异常，打印日志并封装ajax页面提示信息。
            result.setSuccess(false);
            result.setOpMsg(GlobalConstant.QUERRY_ERROR);
            log.error(e.toString() + " : " + e.getMessage());
        }
        return result;
    }

    /**
     * 加载菜单ztree
     * @return 
     * @see
     */
    @RequestMapping(value = "/ztreeMenus.do")
    @ResponseBody
    public List<TreeEntity> ztreeMenus()
    {
        return menuService.ztreeAllMenus();
    }

    /**
     * 新增角色
     * @param request
     * @return 
     * @see
     */
    @RequestMapping(value = "/addRole.do")
    @ResponseBody
    public SimpleBaseResult addRole(HttpServletRequest request)
    {
        SimpleBaseResult result = new SimpleBaseResult();
        try
        {
            String role_name = ServletRequestUtils.getStringParameter(request, "role_name", "");
            String remarks = ServletRequestUtils.getStringParameter(request, "remarks", "");
            String selectMenu = ServletRequestUtils.getStringParameter(request, "selectMenu", "");

            //执行新增操作
            boolean flag = roleService.addRoleOfMenu(role_name, remarks, selectMenu);
            if (flag)
            {
                result.setOpMsg(GlobalConstant.INSERT_SUCCESS);
            }
            else
            {
                result.setSuccess(false);
                result.setOpMsg(GlobalConstant.INSERT_FAILED);
            }
        }
        catch (Exception e)
        {
            //出现异常，则捕获该异常，打印日志并封装ajax页面提示信息。
            result.setSuccess(false);
            result.setOpMsg(GlobalConstant.INSERT_ERROR);
            log.error(e.toString() + " : " + e.getMessage());
        }
        return result;
    }

    /**
     * 获得角色所绑定的菜单名
     * @return 
     * @see
     */
    @RequestMapping(value = "/bindedMenus.do")
    @ResponseBody
    public BaseResult bindedMenus(HttpServletRequest request)
    {
        BaseResult result = new BaseResult();
        try
        {
            String roleId = ServletRequestUtils.getStringParameter(request, "role_id", "");
            String menuIds = roleService.queryMenuIdsOfRole(roleId);
            String menuNames = roleService.queryMenuNamesOfRole(roleId);

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("menuIds", menuIds);
            data.put("menuNames", menuNames);
            result.setData(data);
        }
        catch (Exception e)
        {
            result.setSuccess(false);
            result.setOpMsg(GlobalConstant.QUERRY_ERROR);
            log.error(e.toString() + " : " + e.getMessage());
        }
        return result;
    }

    /**
     * 删除角色
     * @return 
     * @see
     */
    @RequestMapping(value = "/deleteRole.do")
    @ResponseBody
    public SimpleBaseResult deleteRole(HttpServletRequest request)
    {
        SimpleBaseResult result = new SimpleBaseResult();
        try
        {
            String roleId = ServletRequestUtils.getStringParameter(request, "role_id", "");

            //判断角色下是否已经绑定有用户
            if(roleService.isRoleRefUser(roleId))
            {
                result.setSuccess(false);
                result.setOpMsg(GlobalConstant.ROLE_HAS_BIND_USERS);
                return result;
            }
            
            //执行删除操作
            boolean flag = roleService.deleteRole(roleId);
            if (flag)
            {
                result.setOpMsg(GlobalConstant.DELETE_SUCCESS);
            }
            else
            {
                result.setSuccess(false);
                result.setOpMsg(GlobalConstant.DELETE_FAILED);
            }
        }
        catch (Exception e)
        {
            result.setSuccess(false);
            result.setOpMsg(GlobalConstant.DELETE_ERROR);
            log.error(e.toString() + " : " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 修改时，加载菜单ztree
     * @return 
     * @see
     */
    @RequestMapping(value = "/ztreeEditMenus.do")
    @ResponseBody
    public List<TreeEntity> ztreeEditMenus(HttpServletRequest request)
    {
        try
        {
            String roleId = ServletRequestUtils.getStringParameter(request, "role_id", "");
            return menuService.ztreeEditMenus(roleId);
        }
        catch (Exception e)
        {
            log.error(e.toString() + " : " + e.getMessage());
        }
        return null;
    }

    /**
     * 修改角色
     * @param request
     * @return 
     * @see
     */
    @RequestMapping(value = "/editRole.do")
    @ResponseBody
    public SimpleBaseResult editRole(HttpServletRequest request)
    {
        SimpleBaseResult result = new SimpleBaseResult();
        try
        {
            String role_id = ServletRequestUtils.getStringParameter(request, "role_id", "");
            String role_name = ServletRequestUtils.getStringParameter(request, "role_name", "");
            String remarks = ServletRequestUtils.getStringParameter(request, "remarks", "");
            String selectMenu = ServletRequestUtils.getStringParameter(request, "selectMenu", "");

            //执行修改操作
            boolean flag = roleService.updateRoleOfMenu(role_id, role_name, remarks, selectMenu);
            if (flag)
            {
                result.setOpMsg(GlobalConstant.EDIT_SUCCESS);
            }
            else
            {
                result.setSuccess(false);
                result.setOpMsg(GlobalConstant.EDIT_FAILED);
            }
        }
        catch (Exception e)
        {
            //出现异常，则捕获该异常，打印日志并封装ajax页面提示信息。
            result.setSuccess(false);
            result.setOpMsg(GlobalConstant.EDIT_ERROR);
            log.error(e.toString() + " : " + e.getMessage());
        }
        return result;
    }

}
